package codetop.huawei.T54;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 18362
 * @Create: 2022-09-13 0:47:21 星期二
 */
class Solution {

    public static int[] dx = new int[] {0, 1, 0, -1};
    public static int[] dy = new int[] {1, 0, -1, 0};

    public List<Integer> spiralOrder(int[][] matrix) {
        int m = matrix.length, n = matrix[0].length, dir = 0, x = 0, y = 0;
        List<Integer> res = new ArrayList<>();
        while (res.size() < m * n) {
            res.add(matrix[x][y]);
            matrix[x][y] = 101;
            int nx = x + dx[dir], ny = y + dy[dir];
            if (nx < 0 || nx >= m || ny < 0 || ny >= n || matrix[nx][ny] > 100) {
                dir = (dir + 1) % 4;
                nx = x + dx[dir];
                ny = y + dy[dir];
            }
            x = nx;
            y = ny;
        }
        return res;
    }
}